var Document = function Document() {
    //如果不能new  要抛出一个错误
}//构造函数
catvm.safefunction(Document); //tostring 保护

//设置原型链上显示的名字***************************************************************************
Object.defineProperties(Document.prototype, {
    [Symbol.toStringTag]: {
        value: "Document",
        configurable: true
    }
});
// 原型指向原型父亲***************************************************************************
document = {};
document.__proto__ = HTMLDocument.prototype
document.__proto__.__proto__ = Document.prototype
Document.prototype.__proto__ = Node.prototype
//该环境下的属性和方法***************************************************************************
document.readyState = "interactive"  // 纯净 5s  该属性3个值 loading（正在加载），interactive（可交互），complete（完成）
document.location = location
document.hidden = false
document.currentScript = null
document.getElementById = function getElementById(id) {
    catvm.debugger_()
    console.log(id)
    // 从已有元素中通过id寻找元素
    for (let i = 0; i < catvm.domele.length; i++) {
        if (catvm.domele[i].id === id) {
            return catvm.proxy(catvm.domele[i])
        }
    }
    return null
};
catvm.safefunction(document.getElementById)
document.getElementsByTagName = function getElementsByTagName(id) {
    catvm.debugger_()
    switch (id) {
        case "script":
            return [catvm.memory.htmlelements['script']()]
        case "input":
            return [catvm.memory.htmlelements['input']()]
        case "head":
            return [catvm.memory.htmlelements['head']()]
    }
    return null;
};
catvm.safefunction(document.getElementsByTagName)
document.referrer = location.href || '';
document.cookie = ''  // 纯净 5s
document.open = function open() {
    catvm.debugger_()
};
catvm.safefunction(document.open)
document.close = function close() {
    catvm.debugger_()
};
catvm.safefunction(document.close)
document.write = function write(html) {
    var str_len = html.length
    var start_num = -1
    var head_list = []
    var foot_list = []
    var value = ""
    var data_head = undefined
    var data_end = undefined
    while (start_num + 1 <= str_len) {
        start_num += 1
        if (html[start_num] === "<") {
            if (html[start_num + 1] === "/" && data_end === undefined && data_head === undefined) {
                data_end = start_num - 1
                continue
            } else if (data_head === undefined) {
                data_head = start_num
                continue
            } else {
                console.log("出现未知的’<‘")
            }
        }
        if (html[start_num] === ">") {
            if (data_end !== undefined) {
                foot_list.push(value + html.slice(data_end + 1, start_num + 1))
                // console.log(html.slice(data_end, start_num))
                data_end = undefined
                value = ""
                data_head = undefined
                continue
            }
            if (data_head !== undefined) {
                head_list.push(html.slice(data_head, start_num + 1))
                // console.log(html.slice(data_head, start_num+1))
                data_end = undefined
                data_head = undefined
                continue
            } else {
                console.log("出现未知的’>‘")
            }
        }
        if (data_head === undefined && data_end === undefined) {
            value += html[start_num]
        }
    }
    for (let i = head_list.length - 1; i >= 0; i--) {
        var is_ok = false
        for (let j = foot_list.length - 1; j >= 0; j--) {
            var name_head = head_list[i].slice(1).split(" ")[0]
            if (name_head.substr(-1, 1) !== ">") {
                name_head += ">"
            }
            var end_head = foot_list[j].substr(-(name_head.length) - 2, name_head.length + 2)
            if ("</" + name_head === end_head) {
                const dom = catvm.str_dom(head_list[i] + foot_list[j])
                catvm.memory.htmlelements[dom[0]](dom[1]);
                foot_list.remove(foot_list[j])
                head_list.remove(head_list[i])
                is_ok = true
                break
            }
        }
        if (!is_ok) {
            const dom = catvm.str_dom(head_list[i])
            catvm.memory.htmlelements[dom[0]](dom[1]);
        }
    }
    catvm.debugger_()
};
catvm.safefunction(document.write);

document.createComment = function createComment(e) {
    catvm.debugger_()
    return new Comment()
};
catvm.safefunction(document.createComment)
document.createElement = function createElement(ele) {
    catvm.debugger_()
    var tagname = ele.toLowerCase() + "";
    if (catvm.memory.htmlelements[ele] === undefined) {
        debugger;
    }
    const d = catvm.proxy(catvm.memory.htmlelements[ele]())
    d.catvm_name = ele
    return d
};
catvm.safefunction(document.createElement)
document.querySelectorAll = function querySelectorAll(e) {
    catvm.debugger_()
    var li = []
    debugger;
    if (e.split(",").length !== 1) {
        debugger;
    } else {
        for (let i = 0; i < catvm.domele.length; i++) {
            if (catvm.domele[i].catvm_name === e) {
                li.push(e)
            }
        }
        return li
    }

};
catvm.safefunction(document.querySelectorAll)

//代理***************************************************************************
document = catvm.proxy(document)